package com.gouaixin.bee.beeframework2.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.City;
import com.gouaixin.bee.beeframework2.tool.IOUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.gouaixin.bee.beeframework2.extra.xia.datetime.wheelview.ArrayWheelAdapter;
import com.gouaixin.bee.beeframework2.extra.xia.datetime.wheelview.OnWheelChangedListener;
import com.gouaixin.bee.beeframework2.extra.xia.datetime.wheelview.WheelView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Created by Administrator on 2015/3/16.
 */

//应用外，弹出的聊天对话框
public class dialogActivity extends Activity {
    Context context = dialogActivity.this;

    WheelView wv_year, wv_month;

    TextView title;

    //选择器的主题名
    String[] titleArray = {"年龄", "居住地", "身高", "学历", "月收入"};

    //选择器的数据

    //    ArrayList<String> dataList01
    View view;

    String year = "";//年
    String mouth = "";//月

    String[] flag01_left = {"不限", "18岁", "19岁", "20岁", "21岁", "22岁", "23岁", "24岁", "25岁", "26岁", "27岁", "28岁", "29岁", "30岁"};
    String[] flag01_right = {"不限", "18岁", "19岁", "20岁", "21岁", "22岁", "23岁", "24岁", "25岁", "26岁", "27岁", "28岁", "29岁", "30岁"};

    String[] flag02_left;
    String[] flag02_right;

    String[] flag03_left;
    String[] flag03_right;
    String[] flag03_right_old;

    String[] flag04_left = {"不限", "初中及以下", ""};
    String[] flag04_right = {"不限", "18岁", "19岁", "20岁", "21岁", "22岁", "23岁", "24岁", "25岁", "26岁", "27岁", "28岁", "29岁", "30岁"};

    String[] flag05_left = {"不限", "18岁", "19岁", "20岁", "21岁", "22岁", "23岁", "24岁", "25岁", "26岁", "27岁", "28岁", "29岁", "30岁"};
    String[] flag05_right = {"不限", "18岁", "19岁", "20岁", "21岁", "22岁", "23岁", "24岁", "25岁", "26岁", "27岁", "28岁", "29岁", "30岁"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        initData_hight();//准备身高的数据


        //取消标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //初始化布局
        initLayout();


        //根据Intent，准备数据
        initIntent();


        //根据屏幕，决定显示项个数
        int textSize = 0;
        textSize = adjustFontSize(getWindow().getWindowManager());
        wv_month.TEXT_SIZE = textSize;
        wv_year.TEXT_SIZE = textSize;

    }

    private void initData_hight() {
        List<String> list = new ArrayList<String>();
        list.add("不限");
        for (int i = 150; i <= 190; i++) {
            list.add(i + "cm");
        }

        //集合转数组
        flag03_left = (String[]) list.toArray(new String[list.size()]);
        flag03_right = (String[]) list.toArray(new String[list.size()]);

//        mLog.e("flag03_left:"+);
    }

    List<City> datas;
    private void initData_address_left() {
        //读取本地的城市json
        String json = "";
        try {
            json = IOUtils.readStream(getResources().openRawResource(R.raw.city));
            mLog.e("读取本地json：" + json);
        } catch (Exception e) {
            mLog.e("读取本地json：失败" + e.getMessage());
        }
        //json转城市集合
        datas = new Gson().fromJson(json, new TypeToken<List<City>>() {
        }.getType());

        //准备字符串集合
        List<String> strlist = new ArrayList<String>();
        for (int i = 0; i < datas.size(); i++) {
            City province = datas.get(i);
            if (!province.getName().equals("请选择")) {
                strlist.add(province.getName());
            }
        }

        //字符串集合转数组
        flag02_left = (String[]) strlist.toArray(new String[strlist.size()]);

        mLog.e("flag02_left=" + flag02_left.toString());
        mLog.e("flag02_left=" + new Gson().toJson(flag02_left));
    }

    private void initData_address_right(String province_name) {
        //准备字符串集合
        List<String> strlist = new ArrayList<String>();
        for (int i = 0; i < datas.size(); i++) {
            City province = datas.get(i);
            if (province.getName().equals(province_name)) {
                //获取子城市集合
                List<City> citys=province.getSub();
                if(citys!=null){
                    for(int j=0;j<citys.size();j++){
                        City citybean = citys.get(j);
                        if(!citybean.getName().equals("请选择")){
                            strlist.add(citybean.getName());
                        }
                    }
                }
            }
        }

        //字符串集合转数组
        flag02_right = (String[]) strlist.toArray(new String[strlist.size()]);

    }


    /**
     * 初始化布局，按钮的监听
     */
    private void initLayout() {
        view = LayoutInflater.from(context).inflate(R.layout.time_layout, null);
        setContentView(view);

        TextView bt01 = (TextView) view.findViewById(R.id.no);
        TextView bt02 = (TextView) view.findViewById(R.id.ok);
        bt01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        bt02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchIntent();

            }
        });
    }

    private void switchIntent() {
        Intent it = new Intent();
        String str = "";
        switch (flag) {
            case 1:
                str = flag01_left[wv_year.getCurrentItem()] + "~" + flag01_right[wv_month.getCurrentItem()];
                it.putExtra("str", str);
                break;
            case 2:
                str = flag02_left[wv_year.getCurrentItem()] + "~" + flag02_right[wv_month.getCurrentItem()];
                it.putExtra("str", str);
                break;
            case 3:
                str = flag03_left[wv_year.getCurrentItem()] + "~" + flag03_right[wv_month.getCurrentItem()];
                it.putExtra("str", str);
                break;
            case 4:
                str = flag04_left[wv_year.getCurrentItem()] + "~" + flag04_right[wv_month.getCurrentItem()];
                it.putExtra("str", str);
                break;
            case 5:
                str = flag05_left[wv_year.getCurrentItem()] + "~" + flag05_right[wv_month.getCurrentItem()];
                it.putExtra("str", str);
                break;
        }

        setResult(01, it);
        finish();


    }

    /**
     * 判断传入的Intent
     */
    int flag;

    private void initIntent() {

        Intent it = getIntent();
        String flag = it.getStringExtra("flag");
        this.flag = Integer.parseInt(flag);

        //年龄
        if (flag.equals("1")) {
            //标题
            title = (TextView) view.findViewById(R.id.title);
            title.setText(titleArray[0]);

            // 年
            wv_year = (WheelView) view.findViewById(R.id.year);
            wv_year.setAdapter(new ArrayWheelAdapter(flag01_left));// 设置"年"的显示数据
            wv_year.setCyclic(false);// 可循环滚动
            wv_year.setCurrentItem(0);// 初始化时显示的数据

            // 月
            wv_month = (WheelView) view.findViewById(R.id.month);
            wv_month.setAdapter(new ArrayWheelAdapter(flag01_right));
            wv_month.setCyclic(false);
            wv_month.setCurrentItem(0);
        }
//居住地
        if (flag.equals("2")) {
            //标题
            title = (TextView) view.findViewById(R.id.title);
            title.setText(titleArray[1]);

            //准备省的数据
            initData_address_left();


            // 省
            wv_year = (WheelView) view.findViewById(R.id.year);
            wv_year.setAdapter(new ArrayWheelAdapter(flag02_left));// 设置"年"的显示数据
            wv_year.setCyclic(false);// 可循环滚动
            wv_year.setCurrentItem(0);// 初始化时显示的数据


//            //准备市的数据
            initData_address_right(flag02_left[0]);

            // 市
            wv_month = (WheelView) view.findViewById(R.id.month);
            wv_month.setAdapter(new ArrayWheelAdapter(flag02_right));
            wv_month.setCyclic(false);
            wv_month.setCurrentItem(0);


            //当省滚动时，刷新市
            wv_year.addChangingListener(new OnWheelChangedListener() {
                @Override
                public void onChanged(WheelView wheel, int oldValue, int newValue) {
                    //准备市的数据
                    initData_address_right(flag02_left[newValue]);
                    wv_month.setAdapter(new ArrayWheelAdapter(flag02_right));
                    wv_month.setCyclic(false);
                    wv_month.setCurrentItem(0);


                }
            });




        }
//身高
        if (flag.equals("3")) {
            //标题
            title = (TextView) view.findViewById(R.id.title);
            title.setText(titleArray[2]);

            // 年
            wv_year = (WheelView) view.findViewById(R.id.year);
            wv_year.setAdapter(new ArrayWheelAdapter(flag03_left));// 设置"年"的显示数据
            wv_year.setCyclic(false);// 可循环滚动
            wv_year.setCurrentItem(0);// 初始化时显示的数据

            // 月
            wv_month = (WheelView) view.findViewById(R.id.month);
            wv_month.setAdapter(new ArrayWheelAdapter(flag03_right));
            wv_month.setCyclic(false);
            wv_month.setCurrentItem(0);


            flag03_right_old = Arrays.copyOf(flag03_right, flag03_right.length);
            final int length = flag03_right_old.length;


            wv_year.addChangingListener(new OnWheelChangedListener() {
                @Override
                public void onChanged(WheelView wheel, int oldValue, int newValue) {

                    flag03_right = Arrays.copyOfRange(flag03_right_old, newValue, length);
                    wv_month.setAdapter(new ArrayWheelAdapter(flag03_right));
                    wv_month.setCyclic(false);
                    wv_month.setCurrentItem(0);


                }
            });
        }
//学历
        if (flag.equals("4")) {
            //标题
            title = (TextView) view.findViewById(R.id.title);
            title.setText(titleArray[3]);

            // 年
            wv_year = (WheelView) view.findViewById(R.id.year);
            wv_year.setAdapter(new ArrayWheelAdapter(flag01_left));// 设置"年"的显示数据
            wv_year.setCyclic(false);// 可循环滚动
            wv_year.setCurrentItem(0);// 初始化时显示的数据

            // 月
            wv_month = (WheelView) view.findViewById(R.id.month);
            wv_month.setAdapter(new ArrayWheelAdapter(flag01_right));
            wv_month.setCyclic(false);
            wv_month.setCurrentItem(0);
        }
//月收入
        if (flag.equals("5")) {
            //标题
            title = (TextView) view.findViewById(R.id.title);
            title.setText(titleArray[4]);

            // 年
//            flag01_left=new String[]{"不限","小于2000","2000-5000","5000-10000","10000-20000","20000以上"};
            // 年
            wv_year = (WheelView) view.findViewById(R.id.year);
            wv_year.setAdapter(new ArrayWheelAdapter(flag01_left));// 设置"年"的显示数据
            wv_year.setCyclic(false);// 可循环滚动
            wv_year.setCurrentItem(0);// 初始化时显示的数据

            // 月
            wv_month = (WheelView) view.findViewById(R.id.month);
            wv_month.setAdapter(new ArrayWheelAdapter(flag01_right));
            wv_month.setCyclic(false);
            wv_month.setCurrentItem(0);
        }

    }


    public static int adjustFontSize(WindowManager windowmanager) {

        int screenWidth = windowmanager.getDefaultDisplay().getWidth();
        int screenHeight = windowmanager.getDefaultDisplay().getHeight();
         /*  DisplayMetrics dm = new DisplayMetrics();
          dm = windowmanager.getApplicationContext().getResources().getDisplayMetrics();
	     int widthPixels = dm.widthPixels;
	     int heightPixels = dm.heightPixels;
	     float density = dm.density;
	     fullScreenWidth = (int)(widthPixels * density);
	     fullScreenHeight = (int)(heightPixels * density);*/
        if (screenWidth <= 240) { // 240X320 屏幕
            return 10;
        } else if (screenWidth <= 320) { // 320X480 屏幕
            return 14;
        } else if (screenWidth <= 480) { // 480X800 或 480X854 屏幕
            return 24;
        } else if (screenWidth <= 540) { // 540X960 屏幕
            return 26;
        } else if (screenWidth <= 800) { // 800X1280 屏幕
            return 30;
        } else { // 大于 800X1280
            return 30;
        }
    }
}

